<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="classifyName">
        <el-input
          v-model="queryParams.classifyName"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择是否启用" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['storeclassify:storeclassify:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain

          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storeclassify:storeclassify:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain

          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storeclassify:storeclassify:remove']"
        >删除</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['storeclassify:storeclassify:export']"
          v-if="false"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :height="tableHeight"
      :data="storeclassifyList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange">
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="分类名称" align="center" prop="classifyName" />
      <el-table-column label="图标" align="center" prop="classifyImg">

        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.classifyImg" v-if="scope.row.parentId != 0">
        </template>
      </el-table-column>

      <el-table-column label="是否启用" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="stateOptions" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        <el-button
            size="mini"
            type="text"

            @click="handleAddSecond(scope.row)"
            v-if="scope.row.parentId == 0"
            v-hasPermi="['storeclassify:storeclassify:edit']"
          >添加二级分类</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['storeclassify:storeclassify:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleDelete(scope.row)"
            v-hasPermi="['storeclassify:storeclassify:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称" prop="classifyName">
          <el-input v-model="form.classifyName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类图标" prop="classifyImg" v-if="form.parentId != '0'">
          <image-upload v-model="form.classifyImg" :limit="1"></image-upload>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否首页展示">
          <el-radio-group v-model="form.showWechat">
            <el-radio
              v-for="dict in showOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listStoreclassify, getStoreclassify, logicDelStoreclassify, addStoreclassify, updateStoreclassify, exportStoreclassify } from "@/api/storeclassify/storeclassify";

  export default {
    name: "Storeclassify",
    data() {
      return {
        isExpandAll:true,
        tableHeight:0,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 分类表格数据
        storeclassifyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否启用字典
        stateOptions: [],
        showOptions: [],
        // 删除来源字典
        delTypeOptions: [],
        // 创建来源字典
        createTypeOptions: [],
        // 更新来源字典
        updateTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 999,
          parentId: null,
          ancestors: null,
          classifyName: null,
          classifyImg: null,
          state: null,
          orderNum: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
      this.calHeight()
      this.getDicts("sys_yes_no").then(response => {
        this.stateOptions = response.data;
      });
      this.getDicts("sys_yes_no").then(response => {
        this.showOptions = response.data;
      });
      this.getDicts("JY_BUSINESS_TYPE").then(response => {
        this.delTypeOptions = response.data;
      });
      this.getDicts("JY_BUSINESS_TYPE").then(response => {
        this.createTypeOptions = response.data;
      });
      this.getDicts("JY_BUSINESS_TYPE").then(response => {
        this.updateTypeOptions = response.data;
      });
    },
    watch: {
      'showSearch':'calHeight'
    },
    methods: {
      calHeight () {
        this.$nextTick(() => {
          const rect = this.$refs.appcontainer.getBoundingClientRect();
          let height= this.$refs.queryForm.$el.offsetHeight;
          this.tableHeight = rect.height-height-120;
        })
      },
      /** 查询分类列表 */
      getList() {
        this.loading = true;
        listStoreclassify(this.queryParams).then(response => {
          // this.storeclassifyList = response.rows;
          this.storeclassifyList = this.handleTree(response.rows, "id");
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parentId: null,
          ancestors: null,
          classifyName: null,
          classifyImg: null,
          state: "Y",
          showWechat: "Y",
          orderNum: 0,
          delState: null,
          delUser: null,
          delType: null,
          delTime: null,
          createBy: null,
          createType: null,
          createTime: null,
          updateBy: null,
          updateType: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.showSearch=false;
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.form.parentId = 0
        this.open = true;
        this.title = "添加分类";
      },
      handleAddSecond(row){
        this.reset();
        this.form.parentId = row.id
        this.open = true;
        this.title = "添加二级分类";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStoreclassify(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改分类";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStoreclassify(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addStoreclassify(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('确定删除"' + row.classifyName + '"吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelStoreclassify(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有分类数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportStoreclassify(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
